package com.parking.action;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.parking.common.pagination.Page;
import com.parking.common.util.JsonUtil;
import com.parking.entity.ShareParking;
import com.parking.service.IShareParkingService;

@Controller
@RequestMapping(value = "/shareParking")
public class ShareParkingAction {
	
	@Autowired
    @Qualifier("shareParkingService")
    private IShareParkingService shareParkingService;
	
	@RequestMapping(value="find", params="type=jsonpweb")
	public void findJsonpWeb(HttpServletRequest request, HttpServletResponse response) {
		//查询分页页码
		String pn = request.getParameter("page");
		//每页显示记录数量
		String ps = request.getParameter("rows");
		//排序关键字
		String key = request.getParameter("key");
		String areacode = request.getParameter("areacode");
		String type = request.getParameter("t");
		try {
			key = new String(key.getBytes("ISO-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		//jsonp回调函数名称
		String callbackName = request.getParameter("callback");
		if (pn == null || "".equals(pn)) {
			pn = "0";
		}
		if (ps == null || "".equals(ps)) {
			ps = "10";
		}
		if (callbackName == null || "".equals(callbackName)) {
			callbackName = "callback";
		}
		Page<ShareParking> parkingPage = null;
		if (key != null && !"".equals(key)) {
			parkingPage = shareParkingService.findByKeywordAndAreacode(key, Integer.parseInt(areacode), Integer.parseInt(ps), Integer.parseInt(pn), Integer.parseInt(type));
			StringBuffer jsonp = new StringBuffer();
			jsonp.append(callbackName)
			.append("(")
			.append(JsonUtil.objectConverterJson(parkingPage))
			.append(")");
			JsonUtil.sendJsonp(jsonp.toString(), response);
		} else {
			StringBuffer jsonp = new StringBuffer();
			jsonp.append(callbackName)
			.append("({status:0})");
			JsonUtil.sendJsonp(jsonp.toString(), response);
		}
	}
}
